import utils.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/12 10:25
 * @description:
 */
public class Solution199 {
    public List<Integer> rightSideView(TreeNode root) {
        if(root == null){
            return null;
        }
        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode p = queue.poll();
                if (i == size - 1) {
                    res.add(p.val);
                }
                if (p.left != null) {
                    queue.add(p.left);
                }
                if (p.right != null) {
                    queue.add(p.right);
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        TreeNode root = TreeNode.buildTree(new int[]{1, 2, 3, -1, 5, -1, 4});
        Solution199 solution = new Solution199();
        List<Integer> res = solution.rightSideView(root);
        System.out.println(res);
    }
}
